package com.ikas.ai.server.module.data.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.server.aop.Pageable;
import com.ikas.ai.server.module.AbstractBean;
import com.ikas.ai.server.module.InitRedis;
import com.ikas.ai.server.module.data.enums.SignalType;
import com.ikas.ai.server.module.data.model.dto.MeteAddDTO;
import com.ikas.ai.server.module.data.model.dto.MeteUpdateDTO;
import com.ikas.ai.server.module.data.service.MeteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;

/**
 * 测点管理
 *
 * @Author: qi.yilai
 * @Date: 2023/2/2 17:47
 * @Version: 1.0.0
 **/
@RestController
@RequestMapping("/mete")
public class MeteController extends AbstractBean {

    @Autowired
    private MeteService meteService;
    @Autowired
    private InitRedis initRedis;

    /**
     * 新增测点
     *
     * @param meteAddDTO
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@Validated @RequestBody MeteAddDTO meteAddDTO) {
        return JsonResult.ok(meteService.add(meteAddDTO, getUserId()));
    }

    /**
     * 查询该测点被哪个工况流程引用
     *
     * @param id
     * @return
     */
    @GetMapping("/isUsed/{id}")
    public JsonResult isUsed(@PathVariable("id") Long id) {
        return JsonResult.ok(meteService.isUsed(id));
    }


    /**
     * 更新测点
     *
     * @param meteUpdateDTO
     * @return
     */
    @PostMapping("/update")
    public JsonResult update(@Validated @RequestBody MeteUpdateDTO meteUpdateDTO) {
        return JsonResult.ok(meteService.update(meteUpdateDTO, getUserId()));
    }

    /**
     * 根据设备编号获取测点
     *
     * @param equipCode
     * @return
     */
    @GetMapping("/getMetesByEquipmentCode/{equipCode}")
    public JsonResult getMetesByEquipmentCode(@PathVariable("equipCode") String equipCode) {
        return JsonResult.ok(meteService.getMetesByEquipmentCode(equipCode));
    }


    /**
     * 根据机组编号获取测点
     *
     * @param machineNo
     * @return
     */
    @GetMapping("/getMetesByMachineNo/{machineNo}")
    public JsonResult getMetesByMachineNo(@PathVariable("machineNo") String machineNo) {
        return JsonResult.ok(meteService.getMetesByMachineNo(machineNo));
    }

    /**
     * 根据机组编号和ucommon获取测点
     *
     * @param machineNo
     * @return
     */
    @GetMapping("/getMetesByMachineNoAndUcommon/{machineNo}")
    public JsonResult getMetesByMachineNoAndUcommon(@PathVariable("machineNo") String machineNo) {
        return JsonResult.ok(meteService.getMetesByMachineNoAndUcommon(machineNo));
    }

    /**
     * 根据测点类型获取测点
     *
     * @param machineNo
     * @return
     */
    @GetMapping("/getMetesByType")
    public JsonResult getMetesByType(@RequestParam(value = "machineNo", required = false) String machineNo,
                                     @RequestParam(value = "meteType", required = false) Integer meteType,
                                     @RequestParam(value = "common", required = false) Integer common) {
        return JsonResult.ok(meteService.getMetesByType(machineNo, meteType, common));
    }


    /**
     * 分页获取测点
     *
     * @return
     */
    @Pageable(sortType = "desc", sortBy = "createTime")
    @GetMapping("/listMetesByPage")
    public JsonResult listMetesByPage(
            @RequestParam(value = "meteCode", required = false) String meteCode,
            @RequestParam(value = "meteId", required = false) String meteId,
            @RequestParam(value = "signalCnName", required = false) String signalCnName,
            @RequestParam(value = "systemNo", required = false) String systemNo,
            @RequestParam(value = "machineNo", required = false) String machineNo,
            @RequestParam(value = "equipmentCode", required = false) String equipmentCode,
            @RequestParam(value = "meteType") Integer meteType,
            @RequestParam(value = "oid", required = false) Long oid,
            @RequestParam(value = "parentNodeName", required = false) String parentNodeName,
            @RequestParam(value = "nodeName", required = false) String nodeName,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "common", required = false) Integer common) {
        //common=0或null不包含ucommon，common=1包含ucommon
        return JsonResult.ok(meteService.queryMetes(meteCode, meteId, signalCnName, systemNo, machineNo, equipmentCode, meteType, oid, parentNodeName, nodeName, type, common));
    }

    /**
     * 删除测点
     *
     * @param id
     * @return
     */
    @PostMapping("deleteMete/{id}")
    public JsonResult deleteMete(@PathVariable("id") Long id) {
        if (meteService.removeById(id)) {
            initRedis.initBaseMeteCacheAfterDelete(Collections.singletonList(id));
        }
        return JsonResult.ok();
    }

    /**
     * 批量删除测点
     *
     * @param ids
     * @return
     */
    @PostMapping("batchDeleteMete")
    public JsonResult deleteMete(@RequestParam("ids") List<Long> ids) {
        if (meteService.removeByIds(ids)) {
            initRedis.initBaseMeteCacheAfterDelete(ids);
        }
        return JsonResult.ok();
    }

    /**
     * 批量导入测点
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/import")
    public JsonResult importMetes(@RequestParam MultipartFile file, @RequestParam("index") Integer index) throws Exception {
        meteService.importMetes(file, getUserId(), index);
        return JsonResult.ok();
    }

    /**
     * 测点类型列表
     *
     * @return
     */
    @GetMapping("/signalType/list")
    public JsonResult signalTypeList() {
        return JsonResult.ok(SignalType.getTypeList());
    }
}
